<?php
/**
 * (logic)（临时）RemovalOrderDetail移除订单
 */
namespace App\Logic\File\Profit;

use App\Models\Basics\SysCountry;
use App\Models\MonthlyReport\Payment\RemovalOrder;
class ParseRemovalOrderFile
{
    /**
     * 解析RemovalOrder
     * @param $lines
     * @param $date
     * @return int
     */
    public static function exeParse($lines,$date){
        $count = count($lines);
        $i = 0;
        $num = 100;
        $re_count = 0;
        while(true){
            if(empty($lines))break;
            $_lines = array_splice($lines,$i,$num);
            foreach ($_lines as $line){
                $flag = self::_addRemovalOrder($line,$date);
                $flag && $re_count++;
            }
        }
        return $re_count == $count ? 1 : 0;
    }

    private static function _addRemovalOrder($temp,$date){
        if(count($temp) < 6)return true;
        if(!$temp[0])return true;
        $country = strtoupper($temp[1]);
        if($country == 'EU'){
            $countryId = SysCountry::EU_COUNTRY_ID;
        }else{
            $countryId = SysCountry::getIdByCountry($country);
        }

        //RemovalOrderDetail移除订单（临时）
        $re = RemovalOrder::create([
            'report_date' => $date,
            'account' => strtoupper($temp[0]),
            'country' => strtoupper($temp[1]),
            'country_id' => $countryId ?: 0,
            'sku' => $temp[2],
            'disposition' => self::_getDisposition($temp[3]),
            'disposed_quantity' => $temp[4],
            'removal_fee' => $temp[5],
        ]);
        return !$re ? false : true;
    }

    private static $_disposition = ['不可售' => 'Unsellable','可售' => 'Sellable'];

    private static function _getDisposition($disposition){
        if(array_key_exists($disposition,self::$_disposition)){
            return self::$_disposition[$disposition];
        }else{
            return $disposition;
        }
    }
}
